﻿Public Class CrashStatus_Backup

    Public Shared Function Get日期是否存在(ByVal ct As Db.Vb.CTable, _
                                  ByVal dtValue As String) As Boolean
        Try
            Dim sb As New StringBuilder
            Dim bExist As Boolean = False
            sb.AppendLine(" select * from CrashStatusBackup where  日期 = '" + dtValue + "'")
            Dim rs As ADODB.Recordset = ct.ExecuteSql(sb.ToString())
            If rs.EOF Then
                bExist = False
            Else
                bExist = True
            End If
            rs.Close()
            rs = Nothing
            Return bExist
        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Shared Function Get日期是否存在A(ByVal ct As Db.Vb.CTable, _
                                  ByVal dtValue As String) As Boolean
        Try
            Dim sb As New StringBuilder
            Dim bExist As Boolean = False
            sb.AppendLine(" select * from CrashStatusBackup where  日期 >= '" + dtValue + "'")
            Dim rs As ADODB.Recordset = ct.ExecuteSql(sb.ToString())
            If rs.EOF Then
                bExist = False
            Else
                bExist = True
            End If
            rs.Close()
            rs = Nothing
            Return bExist
        Catch ex As Exception
            Return False
        End Try

    End Function

    '保存各银行卡在某个时间的金额 系统日期减去一天 查询数据库存在某条记录 如果不存在全部备份
    Public Shared Sub CrashStatus_BackUp_自动(ByVal ct As Db.Vb.CTable)

        Dim dtValue As String = Now.AddDays(-1).ToString("yyyy-MM-dd")

        If Not Get日期是否存在(ct, dtValue) Then

            'Insert
            Dim dtTable As DataTable = ct.FillTable("select * from  CrashStatus order by TITLE ")
            For index As Integer = 0 To dtTable.Rows.Count - 1

                Dim sbInsert As New StringBuilder
                sbInsert.AppendLine(" Insert into CrashStatusBackup(日期,帐户ID,金额,生成状态) Values(")
                sbInsert.AppendLine("'" & dtValue & "','" & dtTable.Rows(index)("ID") & "','" & dtTable.Rows(index)("Count") & "',0")
                sbInsert.AppendLine(")")

                ct.ExecuteSql(sbInsert.ToString)
            Next

        End If

    End Sub

    Public Shared Sub CrashStatus_BackUp_加(ByVal ct As Db.Vb.CTable, ByVal 金额 As Decimal, ByVal userId As String, ByVal fromDate As String)

        If Get日期是否存在A(ct, fromDate) Then

            Dim sbInsert As New StringBuilder
            sbInsert.AppendLine(" update CrashStatusBackup set 金额 = 金额 + " & 金额 & " where 日期>='" & fromDate & "' AND 帐户ID='" & userId & "'")

            ct.ExecuteSql(sbInsert.ToString)

        End If

    End Sub

    Public Shared Sub CrashStatus_BackUp_减(ByVal ct As Db.Vb.CTable, ByVal 金额 As Decimal, ByVal userId As String, ByVal fromDate As String)

        If Get日期是否存在A(ct, fromDate) Then

            Dim sbInsert As New StringBuilder
            sbInsert.AppendLine(" update CrashStatusBackup set 金额 = 金额 - " & 金额 & " where 日期>='" & fromDate & "' AND 帐户ID='" & userId & "'")

            ct.ExecuteSql(sbInsert.ToString)

        End If

    End Sub

    '备份选择日期如果存在删除  插入 尽量一周手动备份一次
    Public Shared Sub CrashStatus_BackUp_手动(ByVal ct As Db.Vb.CTable, _
                                              ByVal dtValue As String)
        'Delete
        ct.ExecuteSql("delete from CrashStatusBackup where 日期='" & dtValue & "'")

        'Insert
        Dim dtTable As DataTable = ct.FillTable("select * from  CrashStatus order by TITLE ")
        For index As Integer = 0 To dtTable.Rows.Count - 1

            Dim sbInsert As New StringBuilder
            sbInsert.AppendLine(" Insert into CrashStatusBackup(日期,帐户ID,金额,生成状态) Values(")
            sbInsert.AppendLine("'" & dtValue & "','" & dtTable.Rows(index)("ID") & "','" & dtTable.Rows(index)("Count") & "',1")
            sbInsert.AppendLine(")")

            ct.ExecuteSql(sbInsert.ToString)
        Next

    End Sub

    Public Shared Sub CrashStatus_BackUp_手动_删除(ByVal ct As Db.Vb.CTable, _
                                              ByVal dtValue As String)
        'Delete
        ct.ExecuteSql("delete from CrashStatusBackup where 日期='" & dtValue & "'")

    End Sub

    Public Shared Function Get得到结余金额(ByVal ct As Db.Vb.CTable, _
                                           ByVal cashId As String, _
                                           ByVal dtValue As String) As Decimal
        Dim sql As String = " select 金额 from CrashStatusBackup where  日期 = '" + dtValue + "' And 帐户ID = '" & cashId & "'"
        Dim dt As DataTable = ct.FillTable(sql)

        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
            Return dt.Rows(0)("金额")
        End If

        Return 0.0

    End Function
End Class
